package com.example.bujinqi;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button mybtn1,mybtn2;
    TextView myTextview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bujinqi);
        mybtn1 = (Button)findViewById(R.id.btnSub);
        mybtn2 = (Button)findViewById(R.id.btnAdd);
        myTextview = (TextView)findViewById(R.id.num);
        //第一次运行时savedInstanceState是null对象
        if (savedInstanceState != null) {
            Log.i("Life","得到了步进器的值");
            // 通过键恢复对应的值
            myTextview.setText(savedInstanceState.getString("MyNum"));
        }
        mybtn1.setOnClickListener(v->{
            int num = Integer.valueOf(myTextview.getText().toString()) - 1;
            if(num >= 0) {
                myTextview.setText(String.valueOf(num));
            }
        });
        mybtn2.setOnClickListener(v->{
            myTextview.setText(String.valueOf(Integer.valueOf(myTextview.getText().toString()) + 1));
        });
    }

    @Override
    protected void onStart() {

        super.onStart();
        Log.i("Life","Activity即将可见时调用onStart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("Life","Activity销毁时调用onDestroy");
    }
    //当您的Activity开始停止时，系统会调用onSaveInstanceState()(回调)，以便你的Activity可以使用一组键值对来保存状态信息。
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        Log.i("Life","保存了步进器的值");
        outState.putString("MyNum",myTextview.getText().toString());
        super.onSaveInstanceState(outState);
    }
}
